//
//  MainViewController.m
//  NewWeiBo_OC
//
//  Created by 陈竹 on 16/2/21.
//  Copyright © 2016年 陈竹. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "UIImage+Extensions.h"

@interface MainViewController ()
@property(nonatomic,strong)UIButton *comPoseButton;
@end

@implementation MainViewController
-(UIButton *)comPoseButton{
    if (_comPoseButton==nil) {
        _comPoseButton=[[UIButton alloc]init];
        //设置按钮图片
        NSString *imageName=@"tabbar_compose_icon_add";
        [_comPoseButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        NSString *hImageName=[NSString stringWithFormat:@"%@_highlighted",imageName];
        [_comPoseButton setImage:[UIImage imageNamed:hImageName] forState:UIControlStateHighlighted];

        //设置按钮高亮状态图片
        NSString *bgImageName=@"tabbar_compose_button";
        [_comPoseButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
        NSString *hBgImageName=[NSString stringWithFormat:@"%@",bgImageName];
        [_comPoseButton setBackgroundImage:[UIImage imageNamed:hBgImageName] forState:UIControlStateHighlighted];
     
        //设置按钮跟随图片改变大小
        [_comPoseButton sizeToFit];
    }
    return _comPoseButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewControllers];
    [self setupTabbar];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //把按钮方法到视图最顶端
    [self.tabBar bringSubviewToFront:self.comPoseButton];
    
    //算出每个btn的宽度，－1是防止边界碰到
    CGFloat width=(self.tabBar.bounds.size.width/self.childViewControllers.count)-1;
    
    //设置btn左右缩小
    self.comPoseButton.frame=CGRectInset(self.tabBar.bounds, width*2, 0);
    

}
-(void)setupTabbar{
    //下面2个方法同时设置才有用
    //在导航栏上面画一条线
    self.tabBar.shadowImage=[UIImage cz_singleDotImage:[UIColor colorWithWhite:0.9 alpha:1]];
    //设置导航栏的背景图片
    self.tabBar.backgroundImage=[UIImage imageNamed:@"tabbar_background"];

    [self.tabBar addSubview:self.comPoseButton];
}


-(void)addChildViewControllers{
    [self addChildViewController:[[HomeViewController alloc]init] titile:@"首页" imageName:@"tabbar_home"];
     [self addChildViewController:[[MessageViewController alloc]init] titile:@"消息" imageName:@"tabbar_message_center"];
    
    [self addChildViewController:[[UIViewController alloc]init]];
    
     [self addChildViewController:[[DiscoverViewController alloc]init] titile:@"发现" imageName:@"tabbar_discover"];
     [self addChildViewController:[[ProfileViewController alloc]init] titile:@"我" imageName:@"tabbar_profile"];
}

-(void)addChildViewController:(UIViewController*)vc titile:(NSString*)title imageName:(NSString*)imageName{
   //设置标题和按钮文字
    vc.title=title;
    
    //设置文字选中颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    //设置按钮的图片
    vc.tabBarItem.image=[UIImage imageNamed:imageName];
    NSString *imageSelect=[NSString stringWithFormat:@"%@_selected",imageName];
    
    //设置选中图片并且关闭默认的渲染颜色
    vc.tabBarItem.selectedImage=[[UIImage imageNamed:imageSelect] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //设置主控制器
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
    
    //添加
    [self addChildViewController:nav];

}


@end
